package com.lenovo.manufacture;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LightingActivity extends AppCompatActivity {

    private ImageView lightingImg;
    private Button open;
    private Button close;
    private final  Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/Interface/UserFactoryEnvironmentalLog/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lighting);
        initView();
    }

    private void setListener(int i) {
        Network network = retrofit.create(Network.class);
        Call<LightingData> call=network.getLighting(i);
        call.enqueue(new Callback<LightingData>() {
            @Override
            public void onResponse(Call<LightingData> call, Response<LightingData> response) {
                int isLighting = response.body().getData().get(0).getIsLighting();
                Toast.makeText(LightingActivity.this, +isLighting+"", Toast.LENGTH_SHORT).show();
                    if (isLighting==0){
                        lightingImg.setImageResource(R.drawable.lighting);
                    }else {
                        lightingImg.setImageResource(R.drawable.close);
                }
            }
            @Override
            public void onFailure(Call<LightingData> call, Throwable t) {
                Toast.makeText(LightingActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();

            }
        });

    }

    private void initView() {
        lightingImg = (ImageView) findViewById(R.id.lighting_img);
        open = (Button) findViewById(R.id.open);
        close = (Button) findViewById(R.id.close);
        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setListener(1);
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setListener(0);
            }
        });
    }
}